package com.hwadee.controller;

import com.hwadee.mapper.PermissionMapper;
import com.hwadee.pojo.Role;
import com.hwadee.pojo.RoleWithPermissions;
import com.hwadee.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionMapper permissionMapper;
    //获取所有角色列表
    @GetMapping("/list")
    public List<Role> getAllRoles() {
        return roleService.getAllRoles();
    }
    //删除指定 ID 的角色
    @DeleteMapping("/delete/{id}")
    public String deleteRole(@PathVariable Integer id) {
        roleService.deleteRoleById(id); // 这里调用 Service 层的删除方法
        return "success";
    }
    //新增角色，并同时绑定权限
    @PostMapping("/addWithPermissions")
    public int addRoleWithPermissions(@RequestBody RoleWithPermissions req) {
        return roleService.addRoleWithPermissions(req);
    }
    //根据角色 ID 查询该角色所拥有的权限 ID 列表
    @GetMapping("/permissions/{roleId}")
    public List<Integer> getPermissionIdsByRoleId(@PathVariable Integer roleId) {
        return roleService.getPermissionIdsByRoleId(roleId);
    }
    //更新角色及其绑定的权限
    @PostMapping("/update/{roleId}")
    public String updateRoleWithPermissions(@PathVariable Integer roleId, @RequestBody RoleWithPermissions roleData) {
        roleService.updateRoleWithPermissions(roleId, roleData);
        return "success";
    }

}
